<?php

	/**
	 * @package		CoreProject
	 * @subpackage	Core
	 *
	 * cpr_Core_ErrorsClass
	 * 
	 */

	abstract class cpr_Core_ErrorsClass
	{
		/**
		 * @var	array
		 */
		private $_errors = array();

		/**
		 * @return bool
		 */
		public function Validate()
		{
			return count($this->_errors) === 0;
		}

		/**
		 * @param	bool	$_onlyFirstError = false
		 * @return	string
		 */
		public function GetErrors($_onlyFirstError = false)
		{
			$_return = '';
			$_errorsArray = array();
			if (count($this->_errors) > 0)
			{
				if ($_onlyFirstError)
				{
					$_errorsArray[] = $this->_errors[0];
				}
				else
				{
					$_errorsArray = $this->_errors;
				}

				foreach ($_errorsArray as $_variable)
				{
					if (count($_variable) == 2)
					{
						if (null === $_variable[1])
						{
							$_return .= CPR_CRLF.trim($_variable[0]);
						}
						else
						{
							$_return .= CPR_CRLF.trim($_variable[0]).' ('.$_variable[1].')';
						}
					}
				}
			}
			
			return trim($_return);
		}

		/**
		 * @return	string
		 */
		public function GetError()
		{
			return $this->GetErrors(true);
		}

		/**
		 * @param	string	$_str
		 */
		protected function setError($_class, $_str, $_code = null)
		{
			if (strlen($_str) > 0)
			{
				if (null === $_code)
				{
					cpr_Core_Log::Write($_class.': Error - '.$_str);
					$this->_errors[] = array($_str, null);
				}
				else
				{
					$_code = (int) $_code;
					cpr_Core_Log::Write($_class.': Error - '.$_str.' ('.$_code.')');
					$this->_errors[] = array($_str, $_code);
				}
			}
		}
	}
